/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Stones;

import ElementsProperties.Movable;
import Actors.Actor;
import Actors.CollisionType;
import Floors.UnstableBridge;
import Floors.WaterFloor;
import Model.Case;
import Model.CollisionEvent;

/**
 *
 * @author Snowangelic
 */
public class SimpleCrate extends AbstractSolidStone implements Movable  {

    public SimpleCrate(){
       image=EnigmaUtils.ImageLoader.loadImage("./images/simpleCrate.png");
    }

    @Override
    public void update(long dtime) {
        if(myCase.getFloor() instanceof WaterFloor){
            myCase.setFloor(new UnstableBridge());
            myCase.removeStone();
        }
    }

    @Override
    public void actorCollision(CollisionEvent e){
        if(e.getCollisionType()==CollisionType.Up)
            upCollision(e.getActor());
        else if(e.getCollisionType()==CollisionType.Down)
            downCollision(e.getActor());
        else if(e.getCollisionType()==CollisionType.Left)
            leftCollision(e.getActor());
        else if(e.getCollisionType()==CollisionType.Right)
            rightCollision(e.getActor());
        
        super.actorCollision(e);
     }
    
    public void upCollision(Actor actor) {
        if(actor.getDy() > 1000)
            moveDown();
    }

    
    public void downCollision(Actor actor) {
        if(actor.getDy() < -1000)
            moveUp();
    }

   
    public void leftCollision(Actor actor) {
        if(actor.getDx() > 1000)
            moveRight();
    }

    
    public void rightCollision(Actor actor) {
        if(actor.getDx() < -1000)
            moveLeft();
    }

    public void moveUp() {
        Case theCase = myCase.getCase(myCase.getXIndex(), myCase.getYIndex()-1);
        if(theCase!=null){
            if(theCase.getStone()==null){
                myCase.removeStone();
                theCase.setStone(this);
            }
        }
    }

    public void moveDown() {
        Case theCase = myCase.getCase(myCase.getXIndex(), myCase.getYIndex()+1);
        if(theCase!=null){
            if(theCase.getStone()==null){
                myCase.removeStone();
                theCase.setStone(this);
            }
        }
    }

    public void moveLeft() {
        Case theCase = myCase.getCase(myCase.getXIndex()-1, myCase.getYIndex());
        if(theCase!=null){
            if(theCase.getStone()==null){
                myCase.removeStone();
                theCase.setStone(this);
            }
        }
    }

    public void moveRight() {
        Case theCase = myCase.getCase(myCase.getXIndex()+1, myCase.getYIndex());
        if(theCase!=null){
            if(theCase.getStone()==null){
                myCase.removeStone();
                theCase.setStone(this);
            }
        }
    }

}
